package com.wwwarehouse.contract.program_operation.resource_factor_ship;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.GridView;

import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.contract.adapter.SelectResourceFactorShipResourcesAdapter;
import com.wwwarehouse.contract.bean.CustomerAndResListBean;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.SelectResourceFactorShipEvent;

import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * Created by feng.gao on 2017/9/23.
 * 选择资源要素页
 */

public class SelectResourceFactorFragment extends BaseFragment {
	private GridView mGridView;
	private SelectResourceFactorShipResourcesAdapter mSupplyOfResourcesAdapter;
	private List<CustomerAndResListBean.Customers> beanLists = new ArrayList<>();

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		return inflater.inflate(R.layout.supply_of_resources_fragment, container, false);
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		mGridView = (GridView) view.findViewById(R.id.grid_view);

	}

	@Override
	public void setTitle() {
		if (peekFragment() instanceof SelectResourceFactorFragment) {
			mActivity.setTitle(R.string.choose_resources_title);
		}
	}

	@Override
	public void requestDatas() {




	}
	@Override
	public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
		if (enter) {
			if (getArguments() != null && getArguments().getSerializable(ContractConstant.KEY_BUNDLE_VIEW_PAGER_DATA) != null) {
				List<CustomerAndResListBean.Customers> list = (List<CustomerAndResListBean.Customers>) getArguments().getSerializable(ContractConstant.KEY_BUNDLE_VIEW_PAGER_DATA);
				int position = getArguments().getInt(ContractConstant.KEY_BUNDLE_POSITION);
				int pageNum = getArguments().getInt(ContractConstant.KEY_BUNDLE_PAGENUM);
				beanLists.clear();
				for (int i = position * pageNum; i < (position + 1) * pageNum; i++) {
					if (i >= 0 && i <list.size()) {
						beanLists.add(list.get(i));
					}
				}
				mSupplyOfResourcesAdapter = new SelectResourceFactorShipResourcesAdapter(beanLists, mActivity);
				mGridView.setAdapter(mSupplyOfResourcesAdapter);
				mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
						CustomerAndResListBean.Customers customers = beanLists.get(position);
						if (customers.isSelected()) {
							customers.setSelected(false);
						} else {
							customers.setSelected(true);
						}
						mSupplyOfResourcesAdapter.notifyDataSetChanged();
						EventBus.getDefault().post(new SelectResourceFactorShipEvent(customers));
					}
				});
			}
		}
		return null;
	}

}